<template>
	<view class="shoppingCart-wrap " v-if="inquiry">
		<view class="shoppingCart-bg">
			<image class="shoppingCart-bg-pic" src="https://cdn.quantifynet.cn/dti/bg_detail_top@2x.png" mode=""></image>
			<view class="shoppingCart-cart">购物车</view>
			<view class="shoppingCart-bg-count flex-row">
				<view class="shoppingCart-bg-num">共{{ goodsTotal }}件商品</view>
				<view class="shoppingCart-bg-remove" @click="cleanShoppingCart" v-if="list.length > 0">清空</view>
			</view>
		</view>

		<view class="shoppingCart-goods-list" v-if="list">
			<view class="shoppingCart-goods-item" v-for="(item, index) in list" :key="index">
				<view class="goods-item-main flex-row">
					<view class="goods-item-left"><image class="goods-item-blind" :src="item.goods.thumbUrl"></image></view>

					<view class="goods-item-right">
						<view class="goods-name nowrap2">{{ item.goods.name }}</view>
						<view class="goods-word">杨维桢 元代</view>

						<view class="goods-count flex-row">
							<view class="goods-btn flex-row">
								<view class="goods-price">{{ item.goods.salePrice }}</view>
								<view class="goods-unit">元</view>
							</view>

							<view class="goods-item-count flex-row">
								<image
									class="goods-count-sub"
									src="https://cdn.quantifynet.cn/dti/icon_home_reduce@2x.png"
									@click="addOrSubShoppingCart(-1, item.goods.id, item.goods.inventory)"
								></image>
								<view class="goods-count-num">{{ item.num }}</view>
								<image
									class="goods-count-add"
									src="https://cdn.quantifynet.cn/dti/icon_home_add@2x.png"
									@click="addOrSubShoppingCart(1, item.goods.id, item.goods.inventory)"
								></image>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>

		<view class="shoppingCart-bottom flex-row iphonex-footer">
			<view class="shoppingCart-count-txt">合计:</view>
			<view class="shoppingCart-price flex-row">
				<view class="shoppingCart-price-word">￥</view>
				<view class="shoppingCart-price-total">{{ priceTotal }}</view>
			</view>
			<view class="shoppingCart-bottom-pay flex-col" @click="gotoPayment">去支付</view>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			list: null, //购物车列表
			goodsTotal: 0, //总数
			priceTotal: 0,
			order: {}
		};
	},
	onLoad() {},
	onShow() {
		this.findByUserIdShoppingCart();
		this.findTotalShoppingCart();
	},
	onHide() {},
	onUnload() {},
	methods: {
		/**
		 * 查询购物车
		 */
		findByUserIdShoppingCart() {
			this.$api.findByUserIdShoppingCart({}).then(res => {
				if (res.code == 200) {
					if (res.result) {
						this.list = res.result;
					} else {
						this.list = null;
						this.goodsTotal = 0;
						this.priceTotal = 0;
					}
				}
			});
		},

		/**
		 * 查询购物车总数
		 */
		findTotalShoppingCart() {
			this.$api.findTotalShoppingCart({}).then(res => {
				if (res.code == 200) {
					this.goodsTotal = res.result.goodsTotal;
					this.priceTotal = res.result.priceTotal;
				}
			});
		},

		/**
		 * 增减购物车商品数量，返回该商品在购物车中数量
		 * @param {Object} num
		 * @param {Object} id
		 * @param {Object} inventory
		 */
		addOrSubShoppingCart(num, id, inventory) {
			if (inventory <= 0) {
				
				uni.showToast({
					title: '该商品库存不足',
					icon: 'none'
				});
				return;
			}
			this.$api
				.addOrSubShoppingCart({
					data: {
						goodsId: id,
						num: num
					}
				})
				.then(res => {
					if (res.code == 200) {
						this.findByUserIdShoppingCart();
						this.findTotalShoppingCart();
					}
				});
		},

		/**
		 * 清空购物车
		 */
		cleanShoppingCart() {
			uni.showModal({
				title: '您确认要清空购物车吗？',
				success: res => {
					if (res.confirm) {
						this.$api.cleanShoppingCart({}).then(res => {
							if (res.code == 200) {
								this.findByUserIdShoppingCart();
								this.findTotalShoppingCart();
							}
						});
					}
				}
			});
		},

		/**
		 * 跳转支付
		 */
		gotoPayment() {
			this.gotoRouter('/pages-client/store/payment?settlementModel=1');
		}
	}
};
</script>

<style lang="less">
.shoppingCart-wrap {
	box-sizing: border-box;
	padding-bottom: 130rpx;
	.shoppingCart-bottom {
		position: fixed;
		bottom: 0;
		left: 0;
		width: 750rpx;
		height: 116rpx;
		z-index: 999;
		background-color: #fff;
		.shoppingCart-count-txt {
			padding-left: 33rpx;
			color: rgba(13, 13, 13, 1);
			font-size: 30rpx;
			font-family: PingFang-SC-Medium;
			margin-top: 43rpx;
		}
		.shoppingCart-price {
			margin: 22rpx 0 0 8rpx;
			.shoppingCart-price-word {
				font-size: 46rpx;
				font-family: PingFangSC-Semibold;
				color: #c91d1d;
				line-height: 65rpx;
			}
			.shoppingCart-price-total {
				font-size: 54rpx;
				font-family: DINPro-Bold;
				color: #c91d1d;
				line-height: 65rpx;
			}
		}
		.shoppingCart-bottom-pay {
			width: 242rpx;
			height: 116rpx;
			background-color: #891e07;
			margin-left: auto;

			justify-content: center;
			align-items: center;
			color: rgba(255, 255, 255, 1);
			font-size: 32rpx;
			font-family: PingFang-SC-Bold;
		}
	}

	.shoppingCart-bg {
		width: 750rpx;
		height: 254rpx;
		padding: 23rpx 60rpx 0;
		box-sizing: border-box;
		position: relative;
		.shoppingCart-bg-pic {
			width: 750rpx;
			height: 254rpx;
			position: absolute;
			z-index: -1;
			left: 0rpx;
			top: 0rpx;
		}
		.shoppingCart-cart {
			color: rgba(51, 51, 51, 1);
			font-family: PingFang-SC-Bold;
			font-size: 32rpx;
		}

		.shoppingCart-bg-count {
			align-items: center;
			justify-content: space-between;
			margin-top: 10rpx;
			.shoppingCart-bg-num {
				color: rgba(51, 51, 51, 1);
				font-size: 26rpx;
				font-family: PingFang-SC-Regular;
			}
			.shoppingCart-bg-remove {
				color: rgba(51, 51, 51, 1);
				font-size: 28rpx;
				font-family: PingFang-SC-Regular;
			}
		}
	}

	.shoppingCart-goods-list {
		z-index: 1;
		position: relative;
		margin: 0 40rpx;
		transform: translateY(-121rpx);

		.shoppingCart-goods-item {
			background-color: rgba(255, 255, 255, 1);
			border-bottom: 1rpx solid rgba(51, 51, 51, 0.1);
			padding: 20rpx 32rpx;
			box-sizing: border-box;
			margin-bottom: 16rpx;
			width: 670rpx;

			.goods-item-main {
				.goods-item-left {
					width: 150rpx;
					height: 150rpx;
					border-radius: 12rpx;
					border: 1rpx solid rgba(234, 234, 239, 0.5);
					display: flex;
					align-items: center;
					justify-content: center;
					margin-right: 30rpx;
					.goods-item-blind {
						width: 148rpx;
						height: 148rpx;
						border-radius: 12rpx;
					}
					.goods-item-pic {
						width: 146rpx;
						height: 146rpx;
					}
				}

				.goods-item-right {
					width: 424rpx;
					display: flex;
					flex-direction: column;
					.goods-name {
						font-size: 30rpx;
						font-family: STSongti-SC-Black, STSongti-SC;
						font-weight: 900;
						color: #333333;
					}
					.goods-word {
						font-size: 28rpx;
						font-family: STSongti-SC-Regular, STSongti-SC;
						font-weight: 400;
						color: #333333;
						margin-top: 5rpx;
					}

					.goods-count {
						align-items: center;
						justify-content: space-between;
						.goods-btn {
							width: 179rpx;
							height: 54rpx;
							border-radius: 27rpx;
							margin-top: 24rpx;
							align-items: center;
							.goods-price {
								color: #891e07;
								font-size: 34rpx;
								font-family: HYXiaoBoZheZhiTiJ;
							}
							.goods-unit {
								color: #891e07;
								font-size: 24rpx;
								font-family: PingFangSC-Regular;
								margin-top: 4rpx;
							}
						}
						.goods-item-count {
							align-items: center;
							justify-content: space-between;
							padding-top: 20rpx;
							.goods-count-add {
								width: 50rpx;
								height: 50rpx;
							}
							.goods-count-num {
								font-size: 34rpx;
								font-family: DINPro-Bold, DINPro;
								font-weight: bold;
								color: #333333;
								padding: 0 19rpx;
							}
							.goods-count-sub {
								width: 50rpx;
								height: 50rpx;
							}
						}
					}
				}
			}
		}
	}
}
</style>
